#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
const int num=5;
pthread_mutex_t gmutex= PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond= PTHREAD_COND_INITIALIZER;
void *wait(void *args)
{
    std::string name=static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&gmutex);

        pthread_cond_wait(&gcond,&gmutex); //自动解锁
        usleep(10000);
        std::cout << "I am : " << name << std::endl;
        pthread_mutex_unlock(&gmutex);
    }
}
int main()
{
    pthread_t threads[num];
    for (int i = 0; i < num; i++)
    {
        char *name=new char[1024];
        snprintf(name,1024,"thread-%d",i+1);
        pthread_create(threads+i,nullptr,wait,(void*)name);
        usleep(10000);
    }
    sleep(1);
    while(true)
    {
        pthread_cond_signal(&gcond);//只唤醒一个在条件变量等待的线程
//      pthread_cond_broadcast(&gcond); //全部唤醒
        std::cout << "唤醒一个进程。。。" << std::endl;
        sleep(2); 
    }
    for(int i=0;i<num;i++)
    {
        pthread_join(threads[i],nullptr);
    }
}